Page({
  data: {
    product: {
      id: null,
      name: '',
      price: 0,
      image: '',
      recommend: false
    }
  },
  
  onLoad(options) {
    if (options.id) {
      this.loadProduct(options.id);
    }
  },
  
  loadProduct(id) {
    // 从服务器获取商品详情
    // 这里使用模拟数据
    this.setData({
      product: {
        id: id,
        name: `商品${id}`,
        price: Math.floor(Math.random() * 100) + 1,
        image: 'https://via.placeholder.com/150',
        recommend: Math.random() > 0.5
      }
    });
  },
  
  chooseImage() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          'product.image': res.tempFilePaths[0]
        });
      }
    });
  },
  
  submitForm(e) {
    const formData = e.detail.value;
    const product = {
      ...this.data.product,
      ...formData,
      price: parseFloat(formData.price)
    };
    
    // 调用API保存商品
    wx.showToast({
      title: '保存成功',
      icon: 'success',
      duration: 2000,
      success: () => {
        setTimeout(() => {
          wx.navigateBack();
        }, 2000);
      }
    });
  }
})